package com.example.demo.rsa;

import org.apache.commons.codec.binary.Base64;
import org.bouncycastle.asn1.ASN1Object;
import org.bouncycastle.asn1.ASN1ObjectIdentifier;
import org.bouncycastle.asn1.pkcs.PKCSObjectIdentifiers;
import org.bouncycastle.asn1.pkcs.PrivateKeyInfo;
import org.bouncycastle.asn1.x509.AlgorithmIdentifier;
import org.bouncycastle.openssl.PEMKeyPair;
import org.bouncycastle.openssl.PEMParser;
import org.bouncycastle.openssl.jcajce.JcaPEMKeyConverter;

import javax.crypto.Cipher;
import java.io.Reader;
import java.io.StringReader;
import java.security.KeyFactory;
import java.security.PrivateKey;
import java.security.Security;
import java.security.spec.PKCS8EncodedKeySpec;

public class Test5 {

    public static void main(String[] args) throws Exception {
        String publicKey = "-----BEGIN PUBLIC KEY-----\n" +
                "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA5NdJduy5lltBCsDWYitN\n" +
                "Ui8PIvoMWQxu0zfDaDhcyck29oqbXB507SPtweV+g4lIYR8S3Sua2j4AuBQPfLiI\n" +
                "hn0efqV4fXUHnuXTSgXceqxa607IOY8YdKqr9X/OnhlOdsxsvXKYXF6wvj52OWDs\n" +
                "gPk58ovi8qXCNPsy8t1ME+il6GS8mK7xzeyG1O71hlYhk9zgrecc0UwjZZgrQTsr\n" +
                "Ivd7m3OWVZTjOYjLptZt+CRXFqCZRRh3C0/y4VYtI2vAvcbaDLcFKGYQqLUoMdMy\n" +
                "LygtjSW3lzlWQGCDQRYWdsOB+JAS9sYVsztR1RF54T+X4ZMUiZY0tJQjAVxpLkXg\n" +
                "LQIDAQAB\n" +
                "-----END PUBLIC KEY-----\n";


        String privateKey = "-----BEGIN PRIVATE KEY-----\n" +
                "MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQDk10l27LmWW0EK\n" +
                "wNZiK01SLw8i+gxZDG7TN8NoOFzJyTb2iptcHnTtI+3B5X6DiUhhHxLdK5raPgC4\n" +
                "FA98uIiGfR5+pXh9dQee5dNKBdx6rFrrTsg5jxh0qqv1f86eGU52zGy9cphcXrC+\n" +
                "PnY5YOyA+Tnyi+LypcI0+zLy3UwT6KXoZLyYrvHN7IbU7vWGViGT3OCt5xzRTCNl\n" +
                "mCtBOysi93ubc5ZVlOM5iMum1m34JFcWoJlFGHcLT/LhVi0ja8C9xtoMtwUoZhCo\n" +
                "tSgx0zIvKC2NJbeXOVZAYINBFhZ2w4H4kBL2xhWzO1HVEXnhP5fhkxSJljS0lCMB\n" +
                "XGkuReAtAgMBAAECggEABrJnY614aumFvqi3cJnSzyZlNRruZ8a6d/NVHer6vPiZ\n" +
                "/RIMvnl+GeVOYAYoZlukouiTygwsiAyx3ORqMml8eVV9CHjGmc+y2hgT6ShajbtK\n" +
                "HkIo3Hv1dVvI4RjZ6GK8mwACTwyYLKlxI66Hq2GZc9bKg6Oxww2A+ie/YHMdQ4tu\n" +
                "8NUDzOObDC8cNMowKwe1HJJ+QJ0zSaa1dv8o5ghVsRtGo73FXU5ncs0B+NmGYPEw\n" +
                "ccLs7bjdxuU0LwcHO+6z6tDCxDylDxRK/vJudmZnM7CFdWtBV+RNESzkGdGosMO9\n" +
                "GeO9B5ZLWv/EYFr+uPYl9ytDe64uYLoHj3TaotYWcQKBgQDwtRAhYhw0FmHbmPSO\n" +
                "O3A3AWN9oCbalYhf64mEAgigQXhg2be9EouXzj5VMnPpdOFdx6paUeS+w1QI78NF\n" +
                "2/A4rY6fuN6z3dC48TBZDZ3GMsVGPHEfyK33aRFhTawxxTg4atHpul+flUIERIU8\n" +
                "MW+wHjc/C1hrB2goBPb8hUbPRQKBgQDzYToDTMG1IrfF2Z4zm2BFbysSQfl0UkUQ\n" +
                "F9psaQMWXHmt2UPIg2QRDEXVqGOHPXg1DAoVDs5lVDMwwBCUUFBe+dn3lpHeIrFc\n" +
                "/1BMD39NaCC0agm3wWd06IiV1hSLnqBNMaMFrHPzUlJImKCLzf7mWI7bLGjyfk5k\n" +
                "ov1kQ/1HyQKBgCCNbAwHt6a3rC5FhjVCBo/5yRmxMe/3rwTB1swadk4t7Hi0mOuh\n" +
                "+WzjQHzHIPPekafcG8axMo0130SCIFdvQhJ4LkSdfMi4Y8TPIWm5tK+cKB+yht30\n" +
                "hsph1C7jkTj94feHQrIoOUxcpBvtpFMaoG8h9vi4sRFwY/6dhUVrRMM9AoGACLkq\n" +
                "aCXfJ4voBFppdjYSfEqgw5XDyCyOtyXzuUEGUE3eOqH38P7snkPPWPohHx7P6S7W\n" +
                "SazvLuiWsR+hOH+sccsIyfwGnGyn6Mpp6WAA0xhxW6YVVRXEjIctJhm3QM3dtwaZ\n" +
                "bkGhIk6IDnwR/RDUGxN/g1GTLWXjj23cITTipikCgYEAj2EMjh86rwB4DfbYzaVx\n" +
                "Tr2maAv9j3eq17pfuTWMzL2/g3u+0dN4HiyHvsgolhtBY1eygqnUXbcF9hH9Bqo/\n" +
                "iHvvo8SvHXVnSE2lnOqps30pjOkNiN0LDZslQaGMY9yHS7SnQcXmQANT6hm0WY8y\n" +
                "qDcfwmEEaFL9/ZtZY74sBzM=\n" +
                "-----END PRIVATE KEY-----\n";

        String str = "2+5CPqm4v1vgCTwIaDeJPiYeHfN2qhWKm7ocqztjCM3XvbeUQbRmGMLQmPtHjvU+0HXckoSI/xHkxsBMF+M6ln8ZnpIQ0o7QEb2hTHK/Qcj6x+FUoiFF5Sk+jlwDcGEn2VQfzlUqX1WviGx8fGE3SxpPDafo90k7zWzbjEDxNJzPFLJ3Nd1UGvbKhtsecUZA2fRlVpDz8UoKpVjoSDa27tlI8HrkCWF0SnY84dTdlJxN2swrMVDZsTusCHkqVQigfjIXL9Yy3DgsBzdeNB6YXlzz2YzBBAZJZtP67+oQjNVfh8dJxkYKInSXQ7ANhSnw3hHx38bJoECVswYZeyTFgQ==";
        String s1 = privateKey.replaceAll("-----BEGIN PRIVATE KEY-----", "").replaceAll("-----END PRIVATE KEY-----", "");
        String s = RSAUtil.decryptByPriKey(str, s1);
        System.out.println(s);

    }



    public static PrivateKey getPrivateKey(String privateKey) throws Exception {
        Reader privateKeyReader = new StringReader(privateKey);
        PEMParser privatePemParser = new PEMParser(privateKeyReader);
        Object privateObject = privatePemParser.readObject();
        if (privateObject instanceof PEMKeyPair) {
            PEMKeyPair pemKeyPair = (PEMKeyPair) privateObject;
            JcaPEMKeyConverter converter = new JcaPEMKeyConverter().setProvider("BC");
            PrivateKey privKey = converter.getPrivateKey(pemKeyPair.getPrivateKeyInfo());
            return privKey;
        }
        return null;
    }


    public static String decryptRSA(String str, String privateKey) throws Exception {
        Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());
        Cipher rsa = Cipher.getInstance("RSA/ECB/PKCS1Padding", "BC");
        rsa.init(Cipher.DECRYPT_MODE, getPrivateKey(privateKey));
        byte[] utf8 = rsa.doFinal(Base64.decodeBase64(str));
        String result = new String(utf8, "UTF-8");
        return result;
    }

}
